﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chbl.DAL;
using Chbl.Models;

namespace Chbl.Controllers.AdminRole
{
    [Authorize(Roles = "admin")]
    public class AdminTeamPlayerController : Controller
    {
        private readonly ChblDb _db;

        public AdminTeamPlayerController(ChblDb db)
        {
            _db = db;
        }

        //
        // GET: /AdminTeamPlayer/

        public ViewResult Index()
        {
            var teamplayers = _db.TeamPlayers.Include(t => t.Team).Include(t => t.Player);
            return View(teamplayers.ToList());
        }

        //
        // GET: /AdminTeamPlayer/Details/5

        public ViewResult Details(int id)
        {
            TeamPlayer teamplayer = _db.TeamPlayers.Find(id);
            return View(teamplayer);
        }

        //
        // GET: /AdminTeamPlayer/Create

        public ActionResult Create()
        {
            ViewBag.TeamId = new SelectList(_db.Teams, "TeamId", "Name");
            ViewBag.PlayerId = new SelectList(_db.Players, "PlayerId", "Member.FIO");
            return View();
        } 

        //
        // POST: /AdminTeamPlayer/Create

        [HttpPost]
        public ActionResult Create(TeamPlayer teamplayer)
        {
            if (ModelState.IsValid)
            {
                _db.TeamPlayers.Add(teamplayer);
                _db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.TeamId = new SelectList(_db.Teams, "TeamId", "Name", teamplayer.TeamId);
            ViewBag.PlayerId = new SelectList(_db.Players, "PlayerId", "Member.FIO", teamplayer.PlayerId);
            return View(teamplayer);
        }
        
        //
        // GET: /AdminTeamPlayer/Edit/5
 
        public ActionResult Edit(int id)
        {
            TeamPlayer teamplayer = _db.TeamPlayers.Find(id);
            ViewBag.TeamId = new SelectList(_db.Teams, "TeamId", "Name", teamplayer.TeamId);
            ViewBag.PlayerId = new SelectList(_db.Players, "PlayerId", "PlayerId", teamplayer.PlayerId);
            return View(teamplayer);
        }

        //
        // POST: /AdminTeamPlayer/Edit/5

        [HttpPost]
        public ActionResult Edit(TeamPlayer teamplayer)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(teamplayer).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.TeamId = new SelectList(_db.Teams, "TeamId", "Name", teamplayer.TeamId);
            ViewBag.PlayerId = new SelectList(_db.Players, "PlayerId", "PlayerId", teamplayer.PlayerId);
            return View(teamplayer);
        }

        //
        // GET: /AdminTeamPlayer/Delete/5
 
        public ActionResult Delete(int id)
        {
            TeamPlayer teamplayer = _db.TeamPlayers.Find(id);
            return View(teamplayer);
        }

        //
        // POST: /AdminTeamPlayer/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            TeamPlayer teamplayer = _db.TeamPlayers.Find(id);
            _db.TeamPlayers.Remove(teamplayer);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}